package com.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.aop.AuthorizationRequired;
import com.demo.core.constants.HttpStatus;
import com.demo.core.domain.Result;
import com.demo.core.exception.ParameterException;
import com.demo.domain.ScientificDirection;
import com.demo.domain.ro.SearchScientificDirection;
import com.demo.service.ScientificDirectionService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class ScientificDirectionController {
    @Autowired
    private ScientificDirectionService scientificDirectionService;

    /**
     * 得到所有教师的科研方向
     *
     * @return
     * @throws Exception
     */
    @AuthorizationRequired
    @Operation(summary = "得到所有教师科研方向")
    @PostMapping("/getAllTeachersDirection")
    public Result getAllTeachersDirection(HttpServletRequest request) throws Exception {
        if (request == null) {
            throw new ParameterException("request为空");
        }
        Long uid = (Long) request.getAttribute("userId");
        if (uid == null) {
            throw new ParameterException("未登录");
        }
        List<List<ScientificDirection>> list = scientificDirectionService.getAllTeachersDirection();
        return list != null && list.size() > 0 ?
                Result.success(HttpStatus.SUCCESS, "查询成功", list) :
                Result.success(HttpStatus.SUCCESS, "无相关信息", new ArrayList<>());
    }

    /**
     * 得到科研方向，有分页
     *
     * @param current
     * @param size
     * @return
     */
    @AuthorizationRequired
    @Operation(summary = "分页得到所有教师科研方向")
    @RequestMapping("/getDirectionByPage")
    public Result getDirectionByPage(int current, int size, HttpServletRequest request) throws ParameterException {
        if (current <= 0 || size <= 0) {
            throw new ParameterException("分页参数无效");
        }
        if (request == null) {
            throw new ParameterException("request为空");
        }
        Long uid = (Long) request.getAttribute("userId");
        if (uid == null) {
            throw new ParameterException("未登录");
        }
        Page<List<ScientificDirection>> page = scientificDirectionService.getTeaDirectionByPage(current, size);
        return page != null && page.getRecords().size() > 0 ?
                Result.success(HttpStatus.SUCCESS, "查找成功", page) :
                Result.success(HttpStatus.SUCCESS, "无该教师的科研信息", new Page<>());
    }

    /**
     * 得到前几个教师的科研方向
     *
     * @param number
     * @return
     */
    @AuthorizationRequired
    @Operation(summary = "得到前几个教师的科研方向")
    @GetMapping("/getLateDirections")
    public Result getThreeTeaDirection(Integer number, HttpServletRequest request) throws ParameterException {
        if (number == null || number <= 0) {
            throw new ParameterException("未指定需求数量");
        }
        if (request == null) {
            throw new ParameterException("request为空");
        }
        Long uid = (Long) request.getAttribute("userId");
        if (uid == null) {
            throw new ParameterException("未登录");
        }
//            查询
        List<List<ScientificDirection>> res = scientificDirectionService.getFewTeacherDirection(number);
        return res == null || res.size() <= 0 ?
                Result.success(HttpStatus.SUCCESS, "无该教师的科研信息", new ArrayList<>()) :
                Result.success(HttpStatus.SUCCESS, "查找成功", res);
    }

    /**
     * 传递任意长度的教师姓名列表进行搜索
     *
     * @param namelist
     * @return
     */
    @AuthorizationRequired
    @Operation(summary = "得到任意个数教师的科研方向")
    @RequestMapping("/getTeasDirection")
    public Result getTeasDirection(@RequestBody List<String> namelist, HttpServletRequest request) throws ParameterException {
        if (namelist.size() == 0 || namelist == null) {
            throw new ParameterException("nameList为空");
        }
        if (request == null) {
            throw new ParameterException("request为空");
        }
        Long uid = (Long) request.getAttribute("userId");
        if (uid == null) {
            throw new ParameterException("未登录");
        }
        //查询
        List<List<ScientificDirection>> res = scientificDirectionService.getSeveralTeaDirection(namelist);
        return res == null || res.size() == 0 ?
                Result.success(HttpStatus.SUCCESS, "无该教师的科研信息", new ArrayList<>()) :
                Result.success(HttpStatus.SUCCESS, "查找成功", res);
    }


    /**
     * 数据的CRUD
     */


    /**
     * 搜索科研方向
     *
     * @param searchSD
     * @param request
     * @return
     * @throws ParameterException
     */
    @AuthorizationRequired
    @Operation(summary = "搜索科研方向")
    @RequestMapping("/sd/search")
    public Result searchScientificDirection(@RequestBody SearchScientificDirection searchSD, HttpServletRequest request) throws ParameterException {
        if (searchSD == null) {
            throw new ParameterException("searchSD为空");
        }
        if (request == null) {
            throw new ParameterException("request为空");
        }
        Long uid = (Long) request.getAttribute("userId");
        if (uid == null) {
            throw new ParameterException("未登录");
        }
        int current = searchSD.getCurrent();
        int size = searchSD.getSize();
        Page<ScientificDirection> page = new Page<>(current, size);
        //查询
        LambdaQueryWrapper<ScientificDirection> wrapper = scientificDirectionService.searchScientificDirection(searchSD.getSdName(), searchSD.getSdDirectionName());
        scientificDirectionService.page(page, wrapper);
        return Result.success(HttpStatus.SUCCESS, "查找成功", page);
    }

    /**
     * 得到某个科研方向
     *
     * @param sdId
     * @param request
     * @return
     * @throws ParameterException
     */
    @AuthorizationRequired
    @Operation(summary = "得到某个科研方向")
    @RequestMapping("/sd/get")
    public Result getScientificDirectionById(Long sdId, HttpServletRequest request) throws ParameterException {
        if (sdId == null || sdId < 1l) {
            throw new ParameterException("searchSD为空");
        }
        if (request == null) {
            throw new ParameterException("request为空");
        }
        Long uid = (Long) request.getAttribute("userId");
        if (uid == null) {
            throw new ParameterException("未登录");
        }
        ScientificDirection sd = scientificDirectionService.getById(sdId);
        return Result.success(HttpStatus.SUCCESS, "查找成功", sd.getSdIsDelete() == 0 ? sd : null);
    }


    /**
     * 新增科研方向
     *
     * @param sd
     * @param request
     * @return
     * @throws ParameterException
     */
    @AuthorizationRequired
    @Operation(summary = "新增某个科研方向")
    @RequestMapping("/sd/add")
    @Transactional(rollbackFor = Exception.class)
    public Result addScientificDirection(@RequestBody ScientificDirection sd, HttpServletRequest request) throws ParameterException {
        if (sd == null) {
            throw new ParameterException("无新增事项");
        }
        if (request == null) {
            throw new ParameterException("request为空");
        }
        Long uid = (Long) request.getAttribute("userId");
        if (uid == null) {
            throw new ParameterException("未登录");
        }
        if (sd.getSdName() == null || sd.getSdName().isEmpty() || sd.getSdDirection() == null || sd.getSdDirection() < 1 || sd.getSdDirectionName() == null || sd.getSdDirectionName().isEmpty()) {
            throw new ParameterException("信息未填写完整");
        }
        sd.setSdId(null);
        sd.setSdIsDelete(null);
        return Result.success(HttpStatus.SUCCESS, "新增成功", scientificDirectionService.save(sd));
    }

    /**
     * 修改某个科研方向
     *
     * @param scientificDirection
     * @param request
     * @return
     * @throws ParameterException
     */
    @AuthorizationRequired
    @Operation(summary = "修改某个科研方向")
    @RequestMapping("/sd/update")
    public Result updateScientificDirectionById(@RequestBody ScientificDirection scientificDirection, HttpServletRequest request) throws ParameterException {
        if (scientificDirection == null) {
            throw new ParameterException("scientificDirection为空");
        }
        if (request == null) {
            throw new ParameterException("request为空");
        }
        Long uid = (Long) request.getAttribute("userId");
        if (uid == null) {
            throw new ParameterException("未登录");
        }
        if (scientificDirection.getSdId() == null || scientificDirection.getSdId() < 1l) {
            throw new ParameterException("scientificDirectionId错误");
        }
        return Result.success(HttpStatus.SUCCESS, "修改成功", scientificDirectionService.updateById(scientificDirection));
    }


    /**
     * 删除科研方向
     *
     * @param sdIds
     * @param request
     * @return
     * @throws ParameterException
     */
    @AuthorizationRequired
    @Operation(summary = "删除科研方向")
    @RequestMapping("/sd/delete")
    @Transactional(rollbackFor = Exception.class)
    public Result deleteScientificDirection(@RequestBody List<Long> sdIds, HttpServletRequest request) throws ParameterException {
        System.out.println(sdIds);
        if (sdIds == null || sdIds.size() < 1) {
            throw new ParameterException("无删除事项");
        }
        if (request == null) {
            throw new ParameterException("request为空");
        }
        Long uid = (Long) request.getAttribute("userId");
        if (uid == null) {
            throw new ParameterException("未登录");
        }
        List<ScientificDirection> scientificDirections = scientificDirectionService.deleteScientificDirection(sdIds);
        return Result.success(HttpStatus.SUCCESS, "删除成功", scientificDirectionService.updateBatchById(scientificDirections));
    }

}
